//
//  ReadingViewController.m
//  BCProjectB
//
//  Created by lanou3g on 15/12/16.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "ReadingViewController.h"
#import "NetHelper.h"
#import "BCSessonRequst.h"
#import "BCListModel.h"
#import "BCcarouselModel.h"
#import "SDCycleScrollView.h"
#import "CollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "WebViewController.h"
#import "MBProgressHUD.h"
#import "DetailViewController.h"
#import "UIView+Extension.h"
@interface ReadingViewController ()<SDCycleScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *ListCollectionView;
@property(nonatomic,strong)NSMutableArray *lunboModelArr;
@property(nonatomic,strong)NSMutableArray *ListModelArr;

@end

@implementation ReadingViewController
-(NSMutableArray*)lunboModelArr{
    if (!_lunboModelArr) {
        _lunboModelArr=[NSMutableArray array];
    }
    return _lunboModelArr;
}
-(NSMutableArray *)ListModelArr{
    if (!_ListModelArr) {
        _ListModelArr=[NSMutableArray array];
    }
    return _ListModelArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self requstData];
    self.ListCollectionView.delegate=self;
    self.ListCollectionView.dataSource=self;
    [self.ListCollectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    [self defultPutUpView];
 
}
-(void)defultPutUpView{
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    layout.itemSize=CGSizeMake(110, 150);
    layout.scrollDirection=UICollectionViewScrollDirectionVertical;
    layout.sectionInset=UIEdgeInsetsMake(10, 10, 10, 10);
    self.ListCollectionView.collectionViewLayout=layout;
    self.ListCollectionView.backgroundColor=[UIColor whiteColor];

}
-(void)putUpViews{
    NSMutableArray *arr=[NSMutableArray array];
    for (BCcarouselModel *model in self.lunboModelArr) {
        [arr addObject:model.img];
    }
        UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0,64,self.view.frame.size.width, 180)];
    SDCycleScrollView *sdCycleView=[SDCycleScrollView cycleScrollViewWithFrame:view.frame imageURLStringsGroup:arr];
    sdCycleView.delegate=self;
    sdCycleView.placeholderImage=[UIImage imageNamed:@"placeHolder.png"];
      view=sdCycleView;
      [self.view addSubview:view];
   
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.ListModelArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    BCListModel *model=self.ListModelArr[indexPath.row];
    cell.title.text=model.name;
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model.coverimg] placeholderImage:[UIImage imageNamed:@"placeHolder.png"]];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    DetailViewController *detail=[[DetailViewController alloc]init];
    detail.listModel=self.ListModelArr[indexPath.row];
    [self.navigationController pushViewController:detail animated:YES];
    
}

#pragma mark====== SDScrollViewDelegate
-(void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    WebViewController *webView=[[WebViewController alloc]init];
    BCcarouselModel *model=self.lunboModelArr[index];
    webView.url=model.url;
    [self.navigationController pushViewController:webView animated:YES];
}


-(void)requstData{
    if([[BCSessonRequst shareBCsesson]checkNetReturnIsNetingOrNot]==NO){
        UIAlertController *alC=[UIAlertController alertControllerWithTitle:@"网络提醒" message:@"当前没有网络请稍后再试" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *alertAction=[UIAlertAction actionWithTitle:@"取消" style: UIAlertActionStyleCancel handler:nil];
        [alC addAction:alertAction];
        [self presentViewController:alC animated:YES completion:nil];
        return;
    }
    [MBProgressHUD showHUDAddedTo:self.ListCollectionView animated:YES];
    [[BCSessonRequst shareBCsesson]getDataWith:@"http://api2.pianke.me/read/columns" WhenSucess:^(id resqustObj) {
        NSDictionary *dic=resqustObj[@"data"];
        NSArray *lunboArr=dic[@"carousel"];
        for (NSDictionary *dic in lunboArr) {
            BCcarouselModel *model =[[BCcarouselModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.lunboModelArr addObject:model];
        }
        NSArray *list=dic[@"list"];
        for (NSDictionary *dic in list) {
            BCListModel *model=[[BCListModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.ListModelArr addObject:model];
        }
        [self.ListCollectionView reloadData];
        [MBProgressHUD hideAllHUDsForView:self.ListCollectionView animated:YES];
        [self putUpViews];
    } WhenFiled:^(NSError *err) {
        
    }];
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
